package com.adcc.loadSheetForwarder.service;

import com.adcc.loadSheetForwarder.entity.Flight;
import com.adcc.loadSheetForwarder.entity.LoadSheet;
import com.adcc.loadSheetForwarder.utils.LoadSheetException;
import com.adcc.loadSheetForwarder.vo.FlightVO;
import com.adcc.loadSheetForwarder.vo.GetFlightsVO;
import com.adcc.loadSheetForwarder.vo.GetOriginalLoadSheetVO;
import com.adcc.loadSheetForwarder.vo.LoadSheetVO;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class LoadSheetServiceImpl implements LoadSheetService {
    @Autowired
    UmeService umeService;

    @Override
    public List<FlightVO> getFlights(GetFlightsVO getFlightsVO, String traceid) throws LoadSheetException {
        // 整理调用参数
        Map<String, Object> paramMap = Maps.newHashMap();
        paramMap.put("flightNo", getFlightsVO.getFlightNo());
        paramMap.put("flightDate", getFlightsVO.getFlightDate());
        paramMap.put("deptAirport", getFlightsVO.getDeptAirport());
        paramMap.put("arrAirport", Strings.nullToEmpty(getFlightsVO.getArrAirport()));
        paramMap.put("aircraftNo", Strings.nullToEmpty(getFlightsVO.getAircraftNo()));
        paramMap.put("traceid", traceid);

        // 调用接口获取数据
        List<Flight> flightList = umeService.getFlights(paramMap);

        // 整理返回数据
        List<FlightVO> flightVOList = Lists.newArrayList();
        flightList.forEach(flight -> {
            flightVOList.add(flight.toVO());
        });

        return flightVOList;
    }

    @Override
    public LoadSheetVO getOriginalLoadSheet(GetOriginalLoadSheetVO getOriginalLoadSheetVO, String traceid) throws LoadSheetException {
        // 整理调用参数
        Map<String, Object> paramMap = Maps.newHashMap();
        paramMap.put("flightNo", getOriginalLoadSheetVO.getFlightNo());
        paramMap.put("flightDate", getOriginalLoadSheetVO.getFlightDate());
        paramMap.put("deptAirport", getOriginalLoadSheetVO.getDeptAirport());
        paramMap.put("arrAirport", getOriginalLoadSheetVO.getArrAirport());
        paramMap.put("version", getOriginalLoadSheetVO.getVersion());
        String agent = Strings.isNullOrEmpty(getOriginalLoadSheetVO.getAgent()) || "null".equals(getOriginalLoadSheetVO.getAgent()) ? "" : getOriginalLoadSheetVO.getAgent();
        paramMap.put("agent", agent);
        paramMap.put("traceid", traceid);

        // 调用接口获取数据
        LoadSheet loadSheet = umeService.getOriginalLoadSheet(paramMap);
        return loadSheet.toVO();
    }
}
